/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.factory.server.consumers;

import com.factory.server.json.Plan;
import java.util.LinkedList;

/**
 * Stores scheduled tasks queue.
 *
 * @author milos
 */
public class SchedulerTasksQueue {

    private LinkedList<Plan> plans;
    private boolean empty = true;
    private long timeQueued;

    public SchedulerTasksQueue() {
        plans = new LinkedList<Plan>();
        timeQueued = 0;
    }

    public void put(Plan plan) {
        plans.add(plan);
        empty = false;

    }

    public synchronized Plan get() {
        Plan popPlan = plans.pop();
        return popPlan;
    }

    public synchronized boolean isEmpty() {
        return empty;
    }

    public long getTimeQueued() {
        return this.timeQueued;
    }

    public LinkedList<Plan> getPlans() {
        return this.plans;
    }

    @Override
    public SchedulerTasksQueue clone() {
        SchedulerTasksQueue copy = new SchedulerTasksQueue();
        for (Plan plan : plans) {
            Plan planCopy = new Plan(plan.getUserId(), plan.getInputId(), plan.getProblem(), plan.getPrice(), plan.getTimeAsDate(), plan.getPlannedFinishTime(), plan.getComputationTime());
            planCopy.setDeadline(plan.getDeadline());
            copy.put(planCopy);
        }
        return copy;
    }

    public synchronized void setPlans(LinkedList<Plan> plans) {
        this.plans = plans;
    }
}
